package com.jboost.modules.app.service.strategry.hx;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.Strategy;
import com.jboost.modules.app.constant.XmPushEnum;
import com.jboost.modules.app.dto.HsLvPrice;
import com.jboost.modules.app.dto.resp.HsLvPriceResp;
import com.jboost.modules.app.util.StrategyFactory;
import com.jboost.modules.monitor.service.RedisService;
import com.jboost.modules.system.mapper.HsLvPriceMapper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.*;

@Slf4j
@Component
@EnableAsync
@EnableScheduling
public class HsLvPriceJob {
    @Autowired
    HsLvPriceMapper HsLvPriceMapper;
    @Autowired
    RedisService redisService;
    protected HttpHeaders jdyHeaders;
    String add_api = "https://api.jiandaoyun.com/api/v5/app/entry/data/create";

    {
        jdyHeaders = new HttpHeaders();
        jdyHeaders.set("Authorization", "Bearer " + "smLHulipnrxt6Lx9x1GqA0CyzpSl4k2o");

    }

    String lvPriceUrl
            = "https://market.cnal.com/share/market/nh30.json";
    String app_id = "648413a12b740b0008c630a7";
    String form_id = "6436f29159802e00088472e7";

    protected HashMap<String, Object> getParam() {
        HashMap<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("app_id", app_id);
        paramMap.put("entry_id", form_id);
        return paramMap;
    }

    ;
    @Autowired
    StrategyFactory strategyFactory;

    @Async
    @Scheduled(cron = "0 0 16 * * ?")
    public void pull() throws Exception {
        com.jboost.modules.system.entity.HsLvPrice hsLvPrice1 = HsLvPriceMapper.selectOne(Wrappers.lambdaQuery(com.jboost.modules.system.entity.HsLvPrice.class)
                .eq(com.jboost.modules.system.entity.HsLvPrice::getDateTime, DateFormatUtils.format(new Date(), "YYYY-MM-dd")));
        if (!Objects.isNull(hsLvPrice1)){
            return;
        }
        log.info("pull...开始");
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(lvPriceUrl).get()
                .build();
        Response execute = client.newCall(request).execute();
        JSONObject data = (JSONObject) JSONUtil.fromJson(execute.body().string(), Map.class).get("data");
        JSONObject jsonObject = data.getJSONObject("10");
        String price = "";
        Long time = 1L;
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        for (Map.Entry<String, Object> objectEntry : entries) {
            JSONObject value = (JSONObject) objectEntry.getValue();
            if (Long.parseLong(objectEntry.getKey()) > time) {
                price = value.getString("average");
                time = Long.parseLong(objectEntry.getKey());
            }
        }
        HsLvPrice.Insert insert = new HsLvPrice.Insert();
        HashMap<String, Object> dateMap = Maps.newHashMap();
        dateMap.put("value", DateFormatUtils.format(new Date(), "YYYY-MM-dd"));
        insert.setDate(dateMap);
        HashMap<String, Object> priceMap = Maps.newHashMap();
        priceMap.put("value", price);
        insert.setPrice(priceMap);
        HashMap<String, Object> param = getParam();
        param.put("data",insert);
        String post = HttpRestUtils.post(add_api, param, jdyHeaders);
        com.jboost.modules.system.entity.HsLvPrice hsLvPrice = new com.jboost.modules.system.entity.HsLvPrice();
        hsLvPrice.setPrice(Long.valueOf(price));
        hsLvPrice.setDateTime((String) dateMap.get("value"));
        HsLvPriceMapper.insert(hsLvPrice);
        log.info("pull...结束");
    }
}
